<template>
  <div class="app-container">
    <NormalForm pageTitle="字典数据" :operateForm="queryForm" :formList="formList" :enumOpt="enumOpt" @handleQuery="handleQuery" @handleReset="handleReset">
      <template slot="more-btn">
        <el-button type="info" plain @click="goBack">返回</el-button>
      </template>
    </NormalForm>
    <div class="g-card" style="padding: 0 20px 20px 20px">
      <div class="right-btn" style="display: flex; justify-content: space-between; margin-right: 0;">
        <div>
          <el-button type="info" plain icon="el-icon-download" @click="handleExport" v-hasPermi="['system:dict:export']">导出</el-button>
        </div>
        <div>
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPermi="['system:dict:add']">新增</el-button>
          <el-button icon="el-icon-delete" :disabled="currentSelectList.length == 0" @click="handleDelete" v-hasPermi="['system:dict:remove']">删除</el-button>
        </div>
      </div>
      <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="字典编码" align="center" prop="dictCode" />
        <el-table-column label="字典标签" align="center" prop="dictLabel" />
        <el-table-column label="字典键值" align="center" prop="dictValue" />
        <el-table-column label="字典排序" align="center" prop="dictSort" />
        <el-table-column prop="status" label="状态" align="center" min-width="120">
          <template slot-scope="scope">
            <span>{{ stateOpt[scope.row.status] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="120">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:dict:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:dict:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <NormalPagination :currentPage="queryForm.pageNum" :pageSize="queryForm.pageSize" :total="total" @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange"></NormalPagination>
    </div>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="updateVisible" width="500px" append-to-body :close-on-click-modal="false" :show-close="false">
      <el-form ref="updateForm" :model="updateForm" :rules="updateRules" label-width="80px">
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="updateForm.dictType" :disabled="true" />
        </el-form-item>
        <el-form-item label="数据标签" prop="dictLabel">
          <el-input v-model="updateForm.dictLabel" placeholder="请输入数据标签" />
        </el-form-item>
        <el-form-item label="数据键值" prop="dictValue">
          <el-input v-model="updateForm.dictValue" placeholder="请输入数据键值" />
        </el-form-item>
        <el-form-item label="显示排序" prop="dictSort">
          <el-input-number v-model="updateForm.dictSort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="updateForm.status">
            <el-radio v-for="item in enumOpt.stateList" :key="item.dictValue" :label="item.dictValue">
              {{ item.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="updateForm.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateSubmit">确 定</el-button>
        <el-button @click="updateCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listData, getData, delData, addData, updateData } from "@/api/system/dict/data";
import { listType, getType } from "@/api/system/dict/type";
import { downLoadFile } from "@/utils/common";

export default {
  name: "DictDetail",
  data() {
    return {
      queryForm: {
        dictType: null,
        status: null,
        dictLabel: null,
        pageNum: 1,
        pageSize: 20,
      },
      formList: [
        {
          label: '字典名称',
          value: '',
          prop: 'dictType',
          type: 'SELECT',
          emunName: 'dictTypeList'
        },
        {
          label: '字典标签',
          value: '',
          prop: 'dictLabel',
          type: 'INPUT',
          emunName: ''
        },
        {
          label: '数据状态',
          value: '',
          prop: 'status',
          type: 'SELECT',
          emunName: 'stateList'
        },
      ],
      enumOpt: {
        stateList: [],
        dictTypeList: [],
      },
      stateOpt: {},
      dictTypeOpt: {},
      loading: true,
      // 选中数组
      currentSelectList: [],
      total: 0,
      dataList: [],
      // 默认字典类型
      defaultDictType: "",
      title: "",
      updateVisible: false,
      updateForm: {
        dictType: null,
        dictLabel: null,
        dictValue: null,
        dictSort: null,
        status: '0',
        remark: null,
      },
      updateRules: {
        dictLabel: [{ required: true, message: "数据标签不能为空", trigger: "blur" }],
        dictValue: [{ required: true, message: "数据键值不能为空", trigger: "blur" }],
        dictSort: [{ required: true, message: "数据顺序不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    const dictId = this.$route.query && this.$route.query.dictId;
    this.getType(dictId);
    this.getEnumList();
  },
  methods: {
    /** 查询枚举列表 */
    getEnumList() {
      this.getDicts("sys_normal_disable").then((res) => {
        if (res.code == 200) {
          this.enumOpt.stateList = res.data;
          res.data.forEach(item => {
            this.stateOpt[item.dictValue] = item.dictLabel;
          })
        }
      });
      listType().then((res) => {
        if (res.code == 200) {
          res.rows.forEach(item => {
            this.enumOpt.dictTypeList.push({
              dictLabel: item.dictName,
              dictValue: item.dictType,
            })
            this.dictTypeOpt[item.dictType] = item.dictName;
          })
        }
      });
    },
    /** 查询字典类型详细 */
    getType(dictId) {
      getType(dictId).then(res => {
        this.queryForm.dictType = res.data.dictType;
        this.defaultDictType = res.data.dictType;
        this.getTableListFn();
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryForm.pageNum = 1;
      this.getTableListFn();
    },
    /** 重置按钮操作 */
    handleReset() {
      this.queryForm = {
        dictType: this.defaultDictType,
        status: null,
        dictLabel: null,
        pageNum: 1,
        pageSize: 20,
      },
      this.getTableListFn();
    },
    handleSizeChange(val) {
      this.queryForm.pageNum = 1
      this.queryForm.pageSize = val
      this.getTableListFn()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum = val
      this.getTableListFn()
    },
    /** 查询字典数据列表 */
    getTableListFn() {
      this.loading = true;
      listData(this.queryForm).then(res => {
        this.dataList = res.rows || [];
        this.total = res.total || 0;
        this.loading = false;
      });
    },
    // 取消按钮
    updateCancel() {
      this.updateVisible = false;
      this.$refs.updateForm.resetFields();
      this.updateForm = {
        dictType: this.queryForm.dictType,
        dictLabel: null,
        dictValue: null,
        dictSort: null,
        status: '0',
        remark: null,
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.updateVisible = true;
      this.title = "添加字典数据";
      this.updateForm.dictType = this.queryForm.dictType;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.currentSelectList = selection.map(item => item.dictCode)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const dictCode = row.dictCode || this.currentSelectList
      getData(dictCode).then(response => {
        this.updateForm = response.data;
        this.updateVisible = true;
        this.title = "修改字典数据";
      });
    },
    /** 提交按钮 */
    updateSubmit() {
      this.$refs.updateForm.validate(valid => {
        if (valid) {
          if (this.updateForm.dictCode) {
            updateData(this.updateForm).then(response => {
              if (response.code === 200) {
                this.$message.success("修改成功");
                this.updateCancel();
                this.getTableListFn();
              }
            });
          } else {
            addData(this.updateForm).then(response => {
              if (response.code === 200) {
                this.$message.success("新增成功");
                this.updateCancel();
                this.getTableListFn();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictCodes = row.dictCode || this.currentSelectList;
      this.$confirm('是否确认删除字典编码为"' + dictCodes + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delData(dictCodes);
      }).then(() => {
        this.getTableListFn();
        this.$message.success("删除成功");
      }).catch(function () { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const params = {
        dictLabel: this.queryForm.dictLabel,
        dictType: this.queryForm.dictType,
        status: this.queryForm.status,
      }
      downLoadFile('/system/dict/data/export', params, '字典数据列表.xlsx')
    },
    goBack() {
      this.$router.back()
    },
  }
};
</script>

<style lang="scss" scoped>
@import "../../../styles/base.scss";
</style>
